{extend name="../../base/view/common/base" /}
<!-- 主体 -->
{block name="body"}
<div class="p-3">
	<form class="layui-form gg-form-bar border-x border-t">
		<div class="layui-input-inline layui-td-gray" style="width:300px;">
			<input id="year" name="year" autocomplete="off" class="layui-input" value="{$date}" lay-verify="required"
				placeholder="请选择月份" lay-reqText="请选择月份">
		</div>
		<div class="layui-input-inline">
			{if condition="$eid gt 0"}
			<input type="hidden" name="eid" value="{$eid}" lay-verify="required">
			{else/}
			<select name="eid" lay-filter="status">
				<option value="0">系统管理员</option>
				{volist name="enterprise" id="v"}
				<option value="{$v.id}">{$v.title}</option>
				{/volist}
			</select>
			{/if}
		</div>
		<div class="layui-input-inline" style="width:150px;">
			<button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="webform"><i
					class="layui-icon layui-icon-search mr-1"></i>搜索</button>
			<button type="reset" class="layui-btn layui-btn-reset" lay-filter="reset">清空</button>
		</div>
	</form>
	<table class="layui-hide" id="test" lay-filter="test"></table>
</div>

{/block}
<!-- /主体 -->

<!-- 脚本 -->
{block name="script"}
<script>
	const moduleInit = ['tool'];

	//表头工具栏事件

	function gouguInit() {
		var table = layui.table, tool = layui.tool, form = layui.form;

		layui.pageTable = table.render({
			elem: '#test',
			id: 'test',
			toolbar: '#toolbarDemo',
			defaultToolbar: false,
			title: '月总工时列表',
			url: "/home/MonthTime/index",
			page: false,
			cellMinWidth: 80,
			cols: [
				[
					{ field: 'd_name', width: 150, title: '所属企业' },
					{ field: 'year', width: 80, title: '年' },
					{ field: 'month', width: 80, title: '月' },
					{
						field: 'work_hours', title: '当月总工时 <i class="layui-icon layui-icon-tips layui-font-14" ' +
							'lay-event="email-tips" title="该字段开启了编辑功能" style="margin-left: 5px;"></i>',
						fieldTitle: '请输入当月总工时', hide: 0, expandedMode: 'tips', edit: 'work_hours'
					},
				]],
			done: function (res) {
				// console.log(res,"{$eid}")
				// if ("{$eid}" != 0) {
				// 	$("[data-field='d_name']").css('display', 'none');
				// }

				return res;
			}
		});


		// 单元格编辑事件
		table.on('edit(test)', function (obj) {
			console.log(obj)

			// 			eid
			// year  
			// month
			// work_hours
			var id = obj.data.id;
			var eid = `{$eid}`;
			var year = obj.data.year; // 得到字段
			var month = obj.data.month; // 得到修改后的值
			var work_hours = obj.data.work_hours; // 得到所在行所有键值
			// 编辑后续操作，如提交更新请求，以完成真实的数据更新
			// …

			// 其他更新操作
			let callback = function (e) {
				layer.msg(e.msg);
				if (e.code == 0) {
			layer.msg('编辑成功', { icon: 1 });

					layui.pageTable.reload();
				}
			}
			tool.post("/home/MonthTime/add", { id:id, eid: '{$eid}', year:year, month:month, work_hours:work_hours }, callback);
			// obj.update(update);
		});

		table.on('tool(test)', function (obj) {
			if (obj.event === 'edit') {
				tool.side('/home/MonthTime/add?id=' + obj.data.id);
				return;
			}
			if (obj.event === 'disable') {
				layer.confirm('确定要完结该项目吗?', { icon: 3, title: '提示' }, function (index) {
					let callback = function (e) {
						layer.msg(e.msg);
						if (e.code == 0) {
							layui.pageTable.reload();
						}
					}
					tool.post("/home/MonthTime/opertion", { id: obj.data.id, status: 2, title: obj.data.title }, callback);
					layer.close(index);
				});
			}
			if (obj.event === 'open') {
				layer.confirm('确定修改项目状态吗?', { icon: 3, title: '提示' }, function (index) {
					let callback = function (e) {
						layer.msg(e.msg);
						if (e.code == 0) {
							layui.pageTable.reload();
						}
					}
					tool.post("/home/MonthTime/opertion", { id: obj.data.id, status: 1, title: obj.data.title }, callback);
					layer.close(index);
				});
			}

			if (obj.event === 'del') {
				layer.confirm('确定要删除吗？', { icon: 3, title: '提示' }, function (index) {
					let callback = function (e) {
						layer.msg(e.msg);
						if (e.code == 0) {
							layui.pageTable.reload();
						}
					}
					tool.post("/home/MonthTime/delete", { id: obj.data.id }, callback);
					layer.close(index);
				});
			}
		});

		$('body').on('click', '.addNew', function () {
			addExpense(0, '');
		});

		function addExpense(id, val) {
			var title = '新增项目';
			if (id > 0) {
				title = '编辑项目';
			}
			layer.prompt({
				title: title,
				value: val,
				yes: function (index, layero) {
					// 获取文本框输入的值
					var value = layero.find(".layui-layer-input").val();
					if (value) {
						let callback = function (e) {
							layer.msg(e.msg);
							if (e.code == 0) {
								layui.pageTable.reload();
							}
						}
						tool.post("/home/MonthTime/add", { id: id, title: value }, callback);
						layer.close(index);
					} else {
						layer.msg('请填写项目名称');
					}
				}
			})
		}

		//监听搜索提交
		form.on('submit(webform)', function (data) {
			layui.pageTable.reload({ where: data.field });
			return false;
		});

		var form = layui.form,
			tool = layui.tool,
			oaTool = layui.oaTool,
			laydate = layui.laydate;
		//日期时间范围
		laydate.render({
			elem: '#year',
			type: 'year',
			format: 'yyyy',
			showBottom: false
		});
		// form.on('submit(webform)', function (data) {
		//     if (data.field.name) {
		//         layui.pageTable.reload({
		//             where: {
		//                 name: data.field.name,
		//                 status:data.field.status
		//             },
		//             page: {
		//                 curr: 1
		//             }
		//         });
		//     } else {
		//         location.reload();
		//     }
		//     return false;
		// });
	}
</script>
{/block}
<!-- /脚本 -->